package pre.lxm.demo.chart.horizontal;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;


public class HorizontalChart extends View {


    private Paint mPaint;
    private TextPaint mTextPaint;
    private int lineHeight;
    private int leftPadding, rightPadding;

    private int itemHeight;

    public HorizontalChart(Context context) {
        this(context, null);
    }

    public HorizontalChart(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
//        itemWidth = getScreenWidth(getContext()) / 8;
        lineHeight = dip2px(1);
        leftPadding = rightPadding = dip2px(16);
        initPaint();
        initTextPaint();
//        mRecordList = new ArrayList<>();

    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#E1E1E1"));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(lineHeight);
    }

    private void initTextPaint() {
        mTextPaint = new TextPaint();
        mTextPaint.setTextSize(sp2px(14));
        mTextPaint.setStrokeWidth(2);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setStyle(Paint.Style.FILL);
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
//                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
//    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制横线
        itemHeight = getHeight() - lineHeight * 2;
        mPaint.setStrokeWidth(lineHeight);
        mPaint.setColor(Color.parseColor("#E1E1E1"));
        canvas.drawLine(0, lineHeight, getWidth(), lineHeight, mPaint);
        canvas.drawLine(0, lineHeight + itemHeight, getWidth(), lineHeight + itemHeight, mPaint);


        mTextPaint.setColor(Color.parseColor("#888888"));
        float distance;
        float itemWidth = getWidth() / 6;
        Rect mRect = new Rect();
        Paint.FontMetricsInt fontMetrics;
        float baseline;
        for (int i = 1; i < 20; i++) {
            mTextPaint.getTextBounds(String.valueOf(i), 0, 1, mRect);
            fontMetrics = mTextPaint.getFontMetricsInt();
            distance = itemWidth * i;
            baseline = itemHeight / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
            canvas.drawText(String.valueOf(i), distance / 2 - mRect.width() / 2 + distanceX, baseline, mTextPaint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    float lastX = 0;
    float distanceX = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                distanceX += moveX - lastX;
                lastX = moveX;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    private int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    private int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    private static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }
}
